﻿using System;

namespace CY.Utility.Common
{
    /// <summary>
    /// 类型转换类
    /// </summary>
    public class ConvertUtility
    {
        #region 方法

        /**//// <summary>
        /// 判断对象是否为空，如果为空则返回true；反之返回false;
        /// </summary>
        public static bool IsNull(object obj)
        {
            if (obj == null)
            {
                return true;
            }
            return false;
        }

        /**//// <summary>
        /// 将对象转换为string类型，如果对象为空，返回string.Empty
        /// </summary>
        public static string ConvertToString(object obj)
        {
            if (IsNull(obj))
            {
                return string.Empty;
            }
            return obj.ToString();
        }

        /**//// <summary>
        /// 将对象转换为int类型，异常返回默认值
        /// </summary>
        public static int ConvertToInt(object obj, int defaultValue)
        {
            int temp = defaultValue;
            if (IsNull(obj))
            {
                return defaultValue;
            }
            if (int.TryParse(obj.ToString(), out defaultValue))
            {
                return defaultValue;
            }
            return temp;
        }

        /**//// <summary>
        /// 将对象转换为decimal类型，异常返回默认值
        /// </summary>
        public static decimal ConvertToDecimal(object obj, decimal defaultValue)
        {
            decimal temp = defaultValue;
            if (IsNull(obj))
            {
                return defaultValue;
            }
            if (decimal.TryParse(obj.ToString(), out defaultValue))
            {
                return defaultValue;
            }
            return temp;
        }

        /**//// <summary>
        /// 将对象转换为double类型，异常返回默认值
        /// </summary>
        public static double ConvertToDouble(object obj, double defaultValue)
        {
            double temp = defaultValue;
            if (IsNull(obj))
            {
                return defaultValue;
            }
            if (double.TryParse(obj.ToString(), out defaultValue))
            {
                return defaultValue;
            }
            return temp;
        }

        /**//// <summary>
        /// 将对象转换为float类型，异常返回默认值
        /// </summary>
        public static float ConvertToFloat(object obj, float defaultValue)
        {
            float temp = defaultValue;
            if (IsNull(obj))
            {
                return defaultValue;
            }
            if (float.TryParse(obj.ToString(), out defaultValue))
            {
                return defaultValue;
            }
            return temp;
        }

        /**//// <summary>
        /// 将对象转换为DateTime类型，异常返回"0001-01-01"
        /// </summary>
        public static DateTime ConvertToDateTime(object obj)
        {
            DateTime defaultValue;
            if (IsNull(obj))
            {
                return new DateTime(0001,1,1);
            }
            if (DateTime.TryParse(obj.ToString(), out defaultValue))
            {
                return defaultValue;
            }
            return new DateTime(0001, 1, 1);
        }

        public static bool ConvertToBool(object obj, bool defaultValue)
        {
            bool temp = defaultValue;
            if (IsNull(obj))
            {
                return defaultValue;
            }
            if(bool.TryParse(obj.ToString(),out defaultValue))
            {
                return defaultValue;
            }

            return temp;

        }

        public static byte ConvertToByte(object obj, byte defaultValue)
        {
            byte temp = defaultValue;
            if (IsNull(obj))
            {
                return defaultValue;
            }
            if (byte.TryParse(obj.ToString(), out defaultValue))
            {
                return defaultValue;
            }

            return temp;

        }
        #endregion

        public static long ConvertToLong(object obj, long defaultValue)
        {
            long temp = defaultValue;

            if (IsNull(obj))
            {
                return defaultValue;
            }
            if (long.TryParse(obj.ToString(), out defaultValue))
            {
                return defaultValue;
            }
            return temp;
        }
    }
}